<template>
    <div id="sale_records">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{path:'/sale_management/sale_cmd/list'}">销售管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>积分兑换记录</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="searchForm">
            <div class="column">
                <span>订单编号：</span>
                <el-input style="height: 21px;width: 200px" placeholder="请输入兑换订单编号" v-model="searchForm.cn"/>
            </div>
            <div class="column">
                <span>会员账号：</span>
                <el-select v-model="searchForm.memberId"
                           placeholder="请选择会员账号"
                           filterable
                           @change="$forceUpdate()"
                           clearable>
                    <el-option
                            v-for="item in options_memberPhone"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </div>
            <div class="column">
                <span>兑换时间：</span>
                <el-date-picker
                        style="width: 140px"
                        v-model="searchForm.startTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="起始时间">
                </el-date-picker>
                -
                <el-date-picker
                        v-model="searchForm.endTime"
                        style="width: 140px"
                        value-format="yyyy-MM-dd"
                        type="date"
                        placeholder="结束时间">
                </el-date-picker>
            </div>

            <div class="column">
                <el-button type="success" @click="submitSearchForm">搜索</el-button>
            </div>
        </div>
        <div class="table">
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    height="450px"
                    size="medium">
                <el-table-column
                        prop="cn"
                        width="200"
                        label="编号">
                </el-table-column>
                <el-table-column
                        prop="goodsCoverUrl"
                        label="商品封面">
                    <template v-slot="scope">
                        <img :src="scope.row.goodsCoverUrl" alt="兑换商品图片" height="50px"/>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="goodsName"
                        label="商品">
                </el-table-column>
                <el-table-column
                        prop="integral"
                        label="积分">
                </el-table-column>
                <el-table-column
                        width="200"
                        prop="memberPhone"
                        label="会员账号">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        width="200"
                        label="兑换时间">
                </el-table-column>
                <el-table-column
                        prop="updateby"
                        label="操作者">
                </el-table-column>
                <el-table-column
                        width="400"
                        label="操作">
                    <template v-slot="scope">
                        <el-button type="danger"
                                   @click="del(scope.row.cn)" plain>删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pageUtils">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="searchForm.currentPage"
                        :page-sizes="[3, 5, 7, 9]"
                        :page-size="searchForm.pageSize"
                        layout="total,sizes, prev, pager, next,jumper"
                        :total="searchForm.total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
    import {popup} from "@/assets/js/common";
    import {
        delExchangePointProducts,
        queryOptionsMemberPhone,
        queryPageByQoExchangePointProducts
    } from "@/api/sale_management/exchange_point_products/exchangePointProductsApi";

    export default {
        data() {
            return {
                searchForm: {},
                tableData: [],
                options_memberPhone: [],
            }
        },
        methods: {
            queryOptionsMemberPhone() {
                queryOptionsMemberPhone().then(res => {
                    res = res.data
                    if (res.code == 200) {
                        this.options_memberPhone = res.data
                    } else {
                        popup(res.msg, "error")
                    }
                })
            },
            init() {
                if (this.searchForm.startTime > this.searchForm.endTime) {
                    popup("结束时间不能小于开始时间", "warning")
                    return;
                }
                queryPageByQoExchangePointProducts(this.searchForm).then(res => {
                    res = res.data
                    if (res.code == 200) {
                        this.tableData = res.data.records
                        this.searchForm.total = res.data.total
                        this.searchForm.pageSize = res.data.size
                        this.searchForm.currentPage = res.data.current
                    } else {
                        popup(res.msg, "error")
                    }
                })
            },
            del(cn) {
                this.$confirm('确定删除这条记录？', '警示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delExchangePointProducts({cn: cn}).then(res => {
                        res = res.data
                        if (res.code == 200) {
                            popup("操作成功")
                            this.init()
                        } else {
                            popup(res.msg, "error")
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            /*分页*/
            handleSizeChange(val) {
                this.searchForm.pageSize = val
                this.init()
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                this.searchForm.currentPage = val
                this.init()
                console.log(`当前页: ${val}`);
            },
            submitSearchForm() {
                this.init()
            },
        },
        mounted() {
            this.init()
            this.queryOptionsMemberPhone()

        }
    }
</script>
<style>
    .searchForm {
        height: 134px;
    }

    .table {
        background-color: #303133;
        height: 320px;
    }

    .pageUtils {
        position: absolute;
        width: 84%;
        margin: 2px auto;
        height: 43px;
    }

    .searchForm {
        display: inline-block;
        margin: 0 auto;
        height: 51px;
        line-height: 51px;
    }

    .searchForm .column {
        display: inline-block;
        margin: 0 auto;
        margin-right: 2px;
        line-height: 48px;
        height: 51px;
    }
</style>